﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Pager.Entity;

using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    internal enum CustomerDisplay { Search, CreateTime };
    public partial class FrmCustomer : BaseForm
    {
        private CustomerDisplay displayType = CustomerDisplay.Search;
        private int CreateDays = 10;

        public FrmCustomer()
        {
            InitializeComponent();
        }
        public override void RefreshForm()
        {
            BindData();
        }

        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            EnableDate(false);
            InitDictItem();

            BindData();

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
            this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            //if (Portal.gc.HasFunction("Customer/Delete"))
            {
                this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            }
            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;

            //this.btnAddNew.Enabled = Portal.gc.HasFunction("Customer/Add");
            //this.btnEditArea.Enabled = Portal.gc.HasFunction("BasicInfo/CustomerArea");
            //this.btnEditType.Enabled = Portal.gc.HasFunction("BasicInfo/CustomerType");
            //this.menu_BuyTicket.Enabled = Portal.gc.HasFunction("Ticket/Add");
            //this.menu_NewOrder.Enabled = Portal.gc.HasFunction("Order/Add");
            //this.menu_Maintenace.Enabled = Portal.gc.HasFunction("Maintenace");
        }

        private void InitDictItem()
        {
            this.cmbSource.Items.Clear();
            this.cmbSource.Items.AddRange(DictItemUtil.GetCustomerSource());

            this.cmbType.Items.Clear();
            this.cmbType.Items.AddRange(DictItemUtil.GetCustomerType());
        }

        private void EnableDate(bool enable)
        {
            this.dateTimePicker1.Enabled = enable;
            this.dateTimePicker2.Enabled = enable;
        }

        private void chkUseDate_CheckedChanged(object sender, EventArgs e)
        {
            EnableDate(this.chkUseDate.Checked);
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    BLLFactory<Customer>.Instance.Delete(row.Cells["ID"].Value.ToString());
                }
                BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    FrmEditCustomer dlg = new FrmEditCustomer();
                    dlg.ID = row.Cells["ID"].Value.ToString();
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        BindData();
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            btnAddNew_Click(null, null);
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<Customer>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("CustomerType", "宾客类型");
            this.winGridViewPager1.AddColumnAlias("Name", "宾客姓名");
            this.winGridViewPager1.AddColumnAlias("IDCarType", "证件类型");
            this.winGridViewPager1.AddColumnAlias("IDNumber", "证件号码");
            this.winGridViewPager1.AddColumnAlias("Telephone", "联系电话");
            this.winGridViewPager1.AddColumnAlias("Source", "宾客来源");
            this.winGridViewPager1.AddColumnAlias("Sex", "宾客性别");
            this.winGridViewPager1.AddColumnAlias("CompanyName", "公司名称");
            this.winGridViewPager1.AddColumnAlias("Address", "宾客地址");
            this.winGridViewPager1.AddColumnAlias("RegistrationDate", "登记日期");
            this.winGridViewPager1.AddColumnAlias("Note", "备注");
            #endregion

            if (displayType == CustomerDisplay.Search)
            {
                SearchCondition condition = new SearchCondition();
                condition.AddCondition("IDNumber", this.txtIDNumber.Text, SqlOperator.Like)
                    .AddCondition("Name", this.txtName.Text, SqlOperator.Like)
                    .AddCondition("CustomerType", this.cmbType.Text, SqlOperator.Like)
                    .AddCondition("Source", this.cmbSource.Text, SqlOperator.Like)
                    .AddCondition("Address", this.txtAddress.Text, SqlOperator.Like)
                    .AddCondition("CompanyName", this.txtCompany.Text, SqlOperator.Like)
                    .AddCondition("Note", this.txtNote.Text, SqlOperator.Like)
                    .AddCondition("Telephone", this.txtTelephone.Text, SqlOperator.Like, true, "Telephone");
                if (chkUseDate.Checked)
                {
                    condition.AddCondition("RegistrationDate", dateTimePicker1.Value.ToString("yyyy-MM-dd"), SqlOperator.MoreThanOrEqual, true)
                        .AddCondition("RegistrationDate", dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd"), SqlOperator.LessThanOrEqual, true);

                }
                string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
                List<CustomerInfo> list = BLLFactory<Customer>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
                this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<CustomerInfo>(list);
            }
            else
            {
                SearchCondition condition = new SearchCondition();

                //显示在一定时间范围内开户的客户
                if (CreateDays == 0)
                {
                    //30天以外
                    condition.AddCondition("RegistrationDate", DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd"), SqlOperator.LessThanOrEqual, true);
                }
                else
                {
                    condition.AddCondition("RegistrationDate", DateTime.Now.AddDays(-1 * CreateDays).ToString("yyyy-MM-dd"), SqlOperator.MoreThanOrEqual, true)
                        .AddCondition("RegistrationDate", DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"), SqlOperator.LessThanOrEqual, true);
                }
                string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
                this.winGridViewPager1.DataSource = BLLFactory<Customer>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            }

            this.winGridViewPager1.dataGridView1.Refresh();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            displayType = CustomerDisplay.Search ;
            BindData();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            FrmEditCustomer dlg = new FrmEditCustomer();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void menu_New_Click(object sender, EventArgs e)
        {
            btnAddNew_Click(null, null);
        }

        private void menu_NewOrder_Click(object sender, EventArgs e)
        {
            //FrmEditOrder dlg = new FrmEditOrder();
            //string customerNumber = this.winGridViewPager1.dataGridView1.SelectedRows[0].Cells["Number"].Value.ToString();
            //dlg.CustomerNumber = customerNumber;

            //dlg.ShowDialog();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        #region 开户时间范围内

        private void menu_CreateDate_10_Click(object sender, EventArgs e)
        {
            CreateDays = 10;
            displayType = CustomerDisplay.CreateTime;
            BindData();
        }

        private void menu_CreateDate_20_Click(object sender, EventArgs e)
        {
            CreateDays = 20;
            displayType = CustomerDisplay.CreateTime;
            BindData();
        }

        private void menu_CreateDate_30_Click(object sender, EventArgs e)
        {
            CreateDays = 30;
            displayType = CustomerDisplay.CreateTime;
            BindData();
        }

        private void menu_CreateDate_30More_Click(object sender, EventArgs e)
        {
            CreateDays = 0;
            displayType = CustomerDisplay.CreateTime;
            BindData();
        } 
        #endregion

        private void SearchControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }
    }
}
